package com.engine.jucailinkq.attendance.workflow.cmd;

import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.jucailinkq.common.util.DbTools;
import com.engine.core.interceptor.CommandContext;
import lombok.extern.slf4j.Slf4j;
import weaver.general.Util;
import java.util.*;

@Slf4j
public class GetEliminationsEvctionListCmd extends AbstractCommonCommand<Map<String, Object>> {

    public GetEliminationsEvctionListCmd(Map<String, Object> params){
        this.params = params;
    }

    @Override
    public BizLogContext getLogContext() {
        return null;
    }

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {

        Map<String, Object> retmap = new HashMap<String, Object>();
        //获取主表id列表
        String targetUserId = Util.null2String(params.get("targetUserId"));
        String businessTripsType = Util.null2String(params.get("businessTripsType"));
        String startDate = Util.null2String(params.get("startDate"));
        String endDate = Util.null2String(params.get("endDate"));
        log.debug("targetUserId : {}, businessTripsType : {}, startDate : {}, endDate : {}",targetUserId, businessTripsType, startDate, endDate);
        String sql = "select * from uf_jcl_kq_qjjl where qjry="+targetUserId;
        if (!businessTripsType.equals("")) {
            sql += " and jqlx = " + businessTripsType;
        }
        if (!startDate.equals("")) {
            sql += " and ksrq >= '" + startDate+"'";
        }
        if (!endDate.equals("")) {
            sql += " and jsrq <= '" + endDate+"'";
        }
        log.debug(sql);
        List<String> maidList = new ArrayList<>();
        if (!targetUserId.equals("")) {
            List<Map<String,Object>> datas = DbTools.getSqlToList(sql);
            if (datas.size() > 0) {
                datas.forEach(f -> {
                    maidList.add(f.get("id").toString());
                });
            }
        }
        log.debug("maidList : [{}]",maidList);
        //查询子表数据
        if (maidList.size() > 0) {
            String detailSql = "select a.id,a.ksrq,a.kssj,a.jsrq,a.jssj,a.qjsc,a.qtj,a.btj,a.cxqj from uf_jcl_kq_qjjl_dt1 a where mainid in ("+String.join(",",maidList)+")";
            List<Map<String,Object>> detailList = DbTools.getSqlToList(detailSql);
            if (detailList.size() > 0) {
                retmap.put("result", detailList);
            } else {
                retmap.put("result", null);
            }
            log.debug("detailList : [{}]",detailList);
        } else {
            retmap.put("result", null);
        }
        return retmap;
    }
}
